# GUI module for CROSS

#########################################################################################
#
# Copyright (C) 2008  
# Authors: Aljosa Osep (aljosa.osep@gmail.com), Jernej Kranjec (jernej.kranjec@gmail.com)
# From Faculty of Electrical Engineering and Computer Science, Maribor, Slovenia
# Done at School of Technology, Seinajoki, Finland
# Under mentor Kimmo Salmenjoki (kimmo.salmenjoki@seamk.fi)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################################

# ==================== imports ====================== #
import pygtk            		# import pygtk library
pygtk.require('2.0')    		# require version 2.o
import gtk 	        		# import gtk	

import sys

sys.path.append('Modules/Core')
sys.path.append('Modules/GUI/Tabs')
sys.path.append('Modules/GUI/Dialogs')

import commModule

import commandTabModule 		# command tab module
import tagsTabModule
import configTabModule

import connectionDialogModule
import aboutDialogModule
import exceptionModule			# import our custom exceptions module
#import HDLC				# import parser



############################
# GUI class
############################
class RfidCommunicatorGui:		# class definition
	def init_communicator(self):							# constructor(initialization)
		try:
			self.communicator = commModule.Communicator(config=True)			# try to make and Communicator() instance(open port)
		except exceptionModule.SerialPortException, e:				# if comes to SerialPortException, port openning failed
			self.message_box("Error opening serial port! Bad configuration settings or hardware problem. Trying default settings...")
			try:
				self.communicator = commModule.Communicator(config=False)
			except exceptionModule.SerialPortException, e:
				self.message_box("Still doesn't work. It should be hardware problem.")	


	######## methods
	def get_main_menu(self, window):
		accel_group = gtk.AccelGroup()
	
		item_factory = gtk.ItemFactory(gtk.MenuBar, "<main>", accel_group)	# initiates menu factory
		item_factory.create_items(self.menu_items)				# generates menu items

		window.add_accel_group(accel_group)					# add new accelerator group to the window
 		
		self.item_factory = item_factory
		return item_factory.get_widget("<main>")

	######## method adds page to notebook(tab)
	def append_notebook_tab(self, title, frame, notebook):
		label = gtk.Label(title)
		label.show()
		notebook.append_page(frame, label)

	def message_box(self, message):
		self.msg_dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO, gtk.BUTTONS_OK, message)

		self.msg_dialog.run()
		self.msg_dialog.destroy()									


	######## callback functions
	def delete_event(self, widget, event, data=None):				# occurs on attempt to close window
		self.communicator.__del__()
		gtk.main_quit()								# quit program
		return False

	def connection_event(self, widget, data=None):
		connectionWindow = connectionDialogModule.connectionDialog(self.communicator)
		connectionWindow.show()
		return connectionWindow.ret

	def about_event(self, widget, data=None):
		aboutWindow = aboutDialogModule.aboutDialog()
		aboutWindow.show()
		return aboutWindow.ret

	def help_event(self, widget, data=None):
		helpWindow = aboutDialogModule.helpDialog()
		helpWindow.show()
		return helpWindow.ret
			

	# initialization
	def __init__(self):

		# define menu touple
		self.menu_items = (
				( "/_File",         	      None,         None, 0, "<Branch>" ),
				( "/File/Quit",               "<control>Q", gtk.main_quit, 0, None ),

				( "/_Settings",      	      None,	    None, 0, "<Branch>" ),
				( "/Settings/Connection",     None,         self.connection_event, 0, None ),

				( "/_Help",         	      None,         None, 0, "<Branch>" ),
				( "/_Help/Help",   	      None,         self.help_event, 0, None ),
				( "/_Help/About",   	      None,         self.about_event, 0, None ),
			      )

		self.init_communicator()						# init communicator

		######### create window
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)				# makes window
#		self.message_box("Error opening serial port!")
		self.window.connect("delete_event", self.delete_event)  		# connects delete_event to our delete_event callback func
		self.window.set_title("CROSS - CHUMP RFID on Serial Standalone")	# sets window title
		self.window.set_default_size(640, 480)					# sets window default size

		######### define table
		table = gtk.Table(1, 3, False)						# 1x2 grid - for menu and notebook
		self.window.add(table)							# add table to window

		######### menu bar
		menubar = self.get_main_menu(self.window)				# create menubar object
		table.attach(menubar, 0, 1, 0, 1, gtk.FILL, gtk.SHRINK) 		# pack menubar
		menubar.show()								# adds table to window

		######### notebook
		notebook = gtk.Notebook()						# create new notebook
		notebook.set_tab_pos(gtk.POS_TOP)					# notebook tab position
		table.attach(notebook, 0, 1, 1, 2)					# attach notebook to table
		notebook.show()								# show notebook
	#	self.show_tabs = True
	#	self.show_border = True


		######### attach frames to notebook
		self.command_frame = commandTabModule.commandTab(self.communicator)		# new command tab
		self.append_notebook_tab("Command", self.command_frame.get_frame(), notebook)	# append command tab

		self.tag_frame = tagsTabModule.tagsTab(self.communicator)			# new tags tab
		self.append_notebook_tab("Tags", self.tag_frame.get_frame(), notebook)		# append it

		frame = configTabModule.configTab(self.communicator)						# new config tab
		self.append_notebook_tab("Config", frame.get_frame(), notebook)			# and append it

		table.show()									# show table
		self.window.show()								# show whole window

	######## main func
	def main(self):										# class main function
		gtk.main()									# run gtk main
		return 0
